package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Employee;
import org.example.entity.Regularization;
import org.example.models.R;
import org.example.service.EmployeeService;
import org.example.service.RegularizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/regularization")
public class RegularizationController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private RegularizationService regularizationService;
    @GetMapping("/list")
    public R<Page<Regularization>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String workNumber,
            @RequestParam(required = false) String regularizationName,
            @RequestParam(required = false) String department) {
        Page<Regularization> page = new Page<>(pageNum, pageSize);
        return R.success(regularizationService.page(page));
    }
    @PostMapping
    public R add(@RequestBody Regularization regularization) {
        try {
            // 验证必要字段
            if (regularization.getEmployeeIdNumber() == null || regularization.getEmployeeIdNumber().trim().isEmpty()) {
                return R.error("员工工号不能为空");
            }
            // 设置默认值
            if (regularization.getHrConfirm() == null || regularization.getHrConfirm().trim().isEmpty()) {
                regularization.setHrConfirm("待确认");
            }
            boolean result = regularizationService.save(regularization);
            if (result) {
                return R.success("新增成功");
            } else {
                return R.error("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("新增失败: " + e.getMessage());
        }
    }
    @GetMapping("/employee/{employeeIdNumber}")
    public R<Employee> getEmployeeByIdNumber(@PathVariable String employeeIdNumber) {
        try {
            QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("employee_id_number", employeeIdNumber);
            Employee employee = employeeService.getOne(queryWrapper);

            if (employee == null) {
                return R.error("未找到该员工信息");
            }

            // 检查员工状态是否为"待转正"
            if (!"待转正".equals(employee.getEmployeeStatus())) {
                return R.error("该员工状态不是待转正，无法申请转正");
            }

            return R.success(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询员工信息失败: " + e.getMessage());
        }
    }
    @PutMapping("/{id}")
    public R update(@PathVariable Integer id, @RequestBody Regularization regularization) {
        regularization.setRegularizationId(id);
        boolean result = regularizationService.updateById(regularization);
        if (result) {
            return R.success("更新成功");
        } else {
            return R.error("更新失败");
        }
    }
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        try {
            // 先查询记录
            Regularization regularization = regularizationService.getById(id);
            if (regularization == null) {
                return R.error("删除失败，未找到该记录");
            }

            // 检查HR确认状态
            String hrConfirm = regularization.getHrConfirm();
            // 如果已确认或待确认，则不能删除
            if ("已确认".equals(hrConfirm) || "待确认".equals(hrConfirm)) {
                return R.error("该记录已确认或待确认，不能删除");
            }

            // 执行删除
            boolean result = regularizationService.removeById(id);
            if (result) {
                return R.success("删除成功");
            } else {
                return R.error("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("删除失败: " + e.getMessage());
        }
    }
    @PostMapping("/batch-delete")
    public R batchDelete(@RequestBody Map<String, List<Integer>> request) {
        try {
            List<Integer> ids = request.get("ids");

            // 参数校验
            if (ids == null || ids.isEmpty()) {
                return R.error("未提供有效的ID列表");
            }

            // 检查每条记录的HR确认状态
            List<Regularization> records = regularizationService.listByIds(ids);
            for (Regularization record : records) {
                String hrConfirm = record.getHrConfirm();
                // 如果有任何一条记录已确认或待确认，则不能删除
                if ("已确认".equals(hrConfirm) || "待确认".equals(hrConfirm)) {
                    return R.error("存在已确认或待确认的记录，不能删除");
                }
            }

            // 执行批量删除
            boolean result = regularizationService.removeBatchByIds(ids);

            if (result) {
                return R.success("批量删除成功");
            } else {
                return R.error("批量删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("批量删除失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public R<Regularization> getById(@PathVariable Integer id) {
        Regularization regularization = regularizationService.getById(id);
        if (regularization != null) {
            return R.success(regularization);
        } else {
            return R.error("未找到数据");
        }
    }
}

